package com.probox.common.core.utils;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 百度ueditor文本编辑器配置文件编辑类
 * 2014-12-27 by lcx
 * */
public  class UEditorConfigUitl {

	private static String UEDIT_CONFIG_PATH="WEB-INF/classes/";
	private static final String configFileName = "config.json";

	private static JSONObject jsonConfig = null;
	private static  String rootPath;//项目根路径
	private static  String originalPath=null;

	private UEditorConfigUitl(String rootPath)
		throws FileNotFoundException, IOException{
		UEditorConfigUitl.rootPath=rootPath;
		if(ObjectUtil.isEmpty(rootPath)){
			rootPath = rootPath.replace("\\", "/");
		}
		originalPath=rootPath+UEDIT_CONFIG_PATH;
		initEnv(originalPath);
	}

	 public static UEditorConfigUitl getInstance(String rootPath){
		try {
			return new UEditorConfigUitl(rootPath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	 }


	// 读取config.json文件，进行初始化配置
	private static void initEnv(String rootPath) {
		try {
			String configContent = readeFileData(rootPath+configFileName);
			jsonConfig = JSONObject.parseObject(configContent);
		} catch (Exception e) {
			jsonConfig = null;
		}
	}

	public static String readeFileData(String path){
		StringBuilder builder = new StringBuilder();
		try {
			InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "UTF-8");
			BufferedReader bfReader = new BufferedReader(reader);
			String tmpContent = null;
			while ((tmpContent = bfReader.readLine()) != null) {
				builder.append(tmpContent);
			}
			bfReader.close();
			reader.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		return filter(builder.toString());
	}

	private static String filter(String input) {
		return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
	}

	//获取配置文件参数
	public static JSONObject getAllConfig() {
		return jsonConfig;
	}

	public static final int imageAction=1;
	public static final int scrawlAction=2;
	public static final int snapscreenAction=3;
	public static final int catcherAction=4;
	public static final int videoAction=5;

	public static final int fileAction=6;
	public static final int imageManagerAction=7;
	public static final int fileManagerAction=8;

	public static Map<String, Object> getConfig(int type) {
		Map<String,Object> conf = new HashMap<String,Object>();
		String savePath = null;
		UEditorConfigVo vo =new UEditorConfigVo();
		try{
			switch(type){
				case 1://上传图片配置项
					conf.put("isBase64", "false");
					conf.put("imageFieldName", vo.getImageFieldName());
					conf.put("imageMaxSize",Long.valueOf(vo.getImageMaxSize()));
					conf.put("imagePathFormat", vo.getImagePathFormat());
					conf.put("imageCompressEnable", vo.isImageCompressEnable());
					conf.put("imageCompressBorder", Long.valueOf(vo.getImageCompressBorder()));
					conf.put("imageInsertAlign", vo.getImageInsertAlign());
					conf.put("imageAllowFiles", vo.getImageAllowFiles());
					conf.put("imageUrlPrefix", vo.getImageUrlPrefix());
					savePath =vo.getImagePathFormat();
					break;
				case 2://涂鸦图片上传配置项
					conf.put("scrawlFieldName", jsonConfig.getString("scrawlFieldName"));
					conf.put("scrawlPathFormat", jsonConfig.getString("scrawlPathFormat"));
					conf.put("scrawlMaxSize",Long.valueOf(jsonConfig.getLong("scrawlMaxSize")));
					conf.put("scrawlUrlPrefix",jsonConfig.getString("scrawlUrlPrefix"));
					conf.put("scrawlInsertAlign",jsonConfig.getString("scrawlInsertAlign"));
					conf.put("isBase64", "true");
					savePath = jsonConfig.getString("scrawlPathFormat");
					break;
				case 3://截图工具上传
					conf.put("snapscreenActionName", jsonConfig.getString("snapscreenActionName"));
					conf.put("snapscreenPathFormat", jsonConfig.getString("snapscreenPathFormat"));
					conf.put("snapscreenUrlPrefix", jsonConfig.getString("snapscreenUrlPrefix"));
					conf.put("snapscreenInsertAlign",jsonConfig.getString("snapscreenInsertAlign"));
					savePath = jsonConfig.getString("snapscreenPathFormat");
					break;
				case 4://抓取远程图片配置
					conf.put("catcherFieldName", jsonConfig.getString("catcherFieldName"));
					conf.put("catcherPathFormat", jsonConfig.getString("catcherPathFormat"));
					conf.put("catcherUrlPrefix", jsonConfig.getString("catcherUrlPrefix"));
					conf.put("catcherMaxSize",Long.valueOf(jsonConfig.getLong("catcherMaxSize")));
					conf.put("catcherAllowFiles", getArray("catcherAllowFiles"));
					savePath = jsonConfig.getString("catcherPathFormat");
					break;
				case 5://上传视频配置
					conf.put("fieldName", jsonConfig.getString("videoFieldName"));
					conf.put("pathFormat", jsonConfig.getString("videoPathFormat"));
					conf.put("urlPrefix", jsonConfig.getString("videoUrlPrefix"));
					conf.put("maxSize", Long.valueOf(jsonConfig.getLong("videoMaxSize")));
					conf.put("allowFiles", getArray("videoAllowFiles"));
					savePath = jsonConfig.getString("videoPathFormat");
					break;
				case 6://上传文件配置
					conf.put("isBase64", "false");
					conf.put("fieldName", jsonConfig.getString("fileFieldName"));
					conf.put("pathFormat", jsonConfig.getString("filePathFormat"));
					conf.put("urlPrefix", jsonConfig.getString("fileUrlPrefix"));
					conf.put("maxSize",Long.valueOf(jsonConfig.getLong("fileMaxSize")));
					conf.put("allowFiles", getArray("fileAllowFiles"));
					savePath = jsonConfig.getString("filePathFormat");
					break;
				case 7://列出指定目录下的文件
					conf.put("managerListPath", jsonConfig.getString("imageManagerListPath"));
					conf.put("managerListSize", Integer.valueOf(jsonConfig.getString("imageManagerListSize")));
					conf.put("managerUrlPrefix", jsonConfig.getString("imageManagerUrlPrefix"));
//					conf.put("managerInsertAlign", jsonConfig.getString("imageManagerInsertAlign"));
					conf.put("managerAllowFiles", getArray("imageManagerAllowFiles"));
					break;
				case 8://列出指定目录下的图片
					conf.put("managerListPath", jsonConfig.getString("fileManagerListPath"));
					conf.put("managerListSize", Integer.valueOf(jsonConfig.getString("fileManagerListSize")));
					conf.put("managerUrlPrefix", jsonConfig.getString("fileManagerUrlPrefix"));
					conf.put("managerAllowFiles", getArray("fileManagerAllowFiles"));
			}
			conf.put("savePath", savePath);
			conf.put("rootPath", rootPath);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return conf;
	}

	private static String[] getArray(String key) {
		String[] str = null;
		try {
			JSONArray jsonArr = (JSONArray) jsonConfig.get(key);
			int l=jsonArr.size();
			str = new String[l];
			for (int i = 0; i < l; i++) {
				str[i] = jsonArr.getString(i);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return str;
	}


	/**
	 * 获取文件后缀,带“.”
	 * */
	public static String getFileSuffix(String filename) {
		String suffix = "";
		int pos = filename.lastIndexOf('.');
		if (pos > 0 && pos < filename.length() - 1) {
			suffix = filename.substring(pos + 1);
		}
		return "."+suffix;
	}

	public static void main(String[] args) throws Exception {

	}
}
